package com.wandian.audio.mvvm.audiotoprankdetials;

import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.audio.R;
import com.wandian.audio.adapter.AudioTopRankDetailRecycleAdapter;
import com.wandian.audio.bean.MusicTopRankListBean;
import com.wandian.audio.databinding.AudioActivityTopRankDetailsBinding;
import com.wandian.audio.tools.MusicCollector;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 热门排行榜activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_AUDIO_TOP_RANK_DETAILS)
public class AudioTopRankDetailsActivity extends BaseMVVMActivity<AudioTopRankDetailsViewModel> {
    private AudioActivityTopRankDetailsBinding dataBinding;
    private AudioTopRankDetailRecycleAdapter audioTopRankDetailRecycleAdapter;
    private String id;

    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.audio_activity_top_rank_details);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        id = getIntent().getStringExtra("top_rank_id")+"";
        initRecycle();
        initViewData();
        initClink();
        mViewModel.getMusicTopRankDetails(id);
    }

    private void initRecycle(){
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        dataBinding.recycler.setLayoutManager(layoutManager);
        audioTopRankDetailRecycleAdapter  = new AudioTopRankDetailRecycleAdapter(R.layout.audio_item_top_rank_details_view);
        dataBinding.recycler.setAdapter(audioTopRankDetailRecycleAdapter);
        audioTopRankDetailRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                MusicTopRankListBean.PlaylistEntity.TracksEntity bean = (MusicTopRankListBean.PlaylistEntity.TracksEntity)baseQuickAdapter.getData().get(i);
                if(!UIUtils.isEmpty(bean)){
                    Bundle bundle = new Bundle();
                    bundle.putString("music_type","network");
                    bundle.putString("music_id",bean.getId()+"");
                    ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_AUDIO_MAIN).with(bundle).navigation();
                }
            }
        });
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("榜单歌曲");
        mViewModel.listData.observe(this,listBean->{
            if(!UIUtils.isEmpty(listBean)){
                MusicCollector.clearAllSong();
                MusicCollector.setTopRankToSongsList(mViewModel.listData.getValue());
                audioTopRankDetailRecycleAdapter.setNewData(listBean);
            }
        });
    }

    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> {
            finish();
        });
    }



}
